1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.apache.tapestry5.internal.alerts;
16
17 import org.apache.tapestry5.alerts.*;
18 import org.apache.tapestry5.ioc.services.PerThreadValue;
19 import org.apache.tapestry5.ioc.services.PerthreadManager;
20 import org.apache.tapestry5.services.ApplicationStateManager;
21 import org.apache.tapestry5.services.Request;
22 import org.apache.tapestry5.services.ajax.AjaxResponseRenderer;
23 import org.apache.tapestry5.services.ajax.JavaScriptCallback;
24 import org.apache.tapestry5.services.javascript.JavaScriptSupport;
25
26 public class AlertManagerImpl implements AlertManager
27 {
28 private final ApplicationStateManager asm;
29
30 private final Request request;
31
32 private final AjaxResponseRenderer ajaxResponseRenderer;
33
34 private final PerThreadValue<Boolean> needAlertStorageCleanup;
35
36 public AlertManagerImpl(ApplicationStateManager asm, Request request, AjaxResponseRenderer ajaxResponseRenderer, PerthreadManager perThreadManager)
37 {
38 this.asm = asm;
39 this.request = request;
40 this.ajaxResponseRenderer = ajaxResponseRenderer;
41
42 needAlertStorageCleanup = perThreadManager.createValue();
43 }
44
45 public void success(String message)
46 {
47 alert(Duration.SINGLE, Severity.SUCCESS, message);
48 }
49
50 public void info(String message)
51 {
52 alert(Duration.SINGLE, Severity.INFO, message);
53 }
54
55 public void warn(String message)
56 {
57 alert(Duration.SINGLE, Severity.WARN, message);
58 }
59
60 public void error(String message)
61 {
62 alert(Duration.SINGLE, Severity.ERROR, message);
63 }
64
65 public void alert(Duration duration, Severity severity, String message)
66 {
67 alert(duration, severity, message, false);
68 }
69
70 public void alert(Duration duration, Severity severity, String message, boolean markup)
71 {
72 final Alert alert = new Alert(duration, severity, message, markup);
73
74 if (request.isXHR())
75 {
76 addCallbackForAlert(alert);
77 }
78
79
80
81
82
83 getAlertStorage().add(alert);
84 }
85
86 private void addCallbackForAlert(final Alert alert)
87 {
88 ajaxResponseRenderer.addCallback(new JavaScriptCallback()
89 {
90 public void run(JavaScriptSupport javascriptSupport)
91 {
92 javascriptSupport.require("t5/core/alert").with(alert.toJSON());
93 }
94 });
95
96 addAlertStorageCleanupCallback();
97 }
98
99 private void addAlertStorageCleanupCallback()
100 {
101
102
103
104 if (needAlertStorageCleanup.get(true))
105 {
106 ajaxResponseRenderer.addCallback(new JavaScriptCallback()
107 {
108 public void run(JavaScriptSupport javascriptSupport)
109 {
110
111
112
113 getAlertStorage().dismissNonPersistent();
114 }
115 });
116
117 needAlertStorageCleanup.set(false);
118 }
119 }
120
121 private AlertStorage getAlertStorage()
122 {
123 return asm.get(AlertStorage.class);
124 }
125
126 }